﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

//------------------------------------------------------
//クラス：H3A1 07
//名　前：仮屋　智央
//内　容：講師のカードをかざしログインする画面
//　　　：
//------------------------------------------------------


namespace h3a1_felica2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 上の例に加え、太字のスタイルを設定する
            this.label1.Font = new Font("Arial", 12, FontStyle.Bold);
            //フォームのスタイル指定
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.Text = "ログイン";
            this.label2.Text = "";
            this.label3.Text = "出席記録システム";
            this.label3.Font = new Font("Arial", 20, FontStyle.Bold);

            //タイマー設定
            this.timer1.Interval = 500;
            this.timer1.Enabled = true;
        }


        public void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                using (FelicaLib.Felica f = new FelicaLib.Felica())
                {
                    string uri = System.Configuration.ConfigurationSettings.AppSettings["uri"];
                    string database = System.Configuration.ConfigurationSettings.AppSettings["database"];

                    f.Polling(0xFFFF);
                    byte[] data = f.IDm();

                    string dataStr = "";
                    for (int i = 0; i < data.Length; i++)
                    {
                        dataStr += data[i].ToString("X2");
                    }

                    try
                    {
                        MySqlConnection cn;
                        MySqlCommand cmd;
                        MySqlDataReader reader;
                        cn = new MySqlConnection();
                        cn.ConnectionString = "Server="+uri+";Uid=root;Pwd=mysql;database=" + database + ";";
                        cmd = new MySqlCommand();
                        cmd.Connection = cn;
                        cn.Open();
                        cmd.CommandText = "select lecturer_id,lecturer_name from lecturer where felica_id ='" + dataStr + "';";
                        reader = cmd.ExecuteReader();
                        
                        int i = 0;

                        while (reader.Read())
                        {
                            Parameter.lecturer_id =int.Parse(reader.GetString(0));
                            Parameter.lecturer_name = reader.GetString(1);
                            i++;
                        }

                        if (i == 0)
                        {
                            this.label2.Text = "そのカードは登録されていません";
                            SoundUtils su = new SoundUtils();
                            su.PlaySound("ng");

                            //一秒間（500ミリ秒）停止する
                            System.Threading.Thread.Sleep(500);
                        }
                        else //認証成功の時
                        {
                            //タイマー処理停止
                            this.timer1.Enabled = false;

                            //認証成功の音を出力
                            SoundUtils su = new SoundUtils();
                            su.PlaySound("ok");

                            //ログイン者の名前を格納
                            //Parameter.lecturer_name = name_str;

                            //座標の取得
                            int map_x = this.Location.X;
                            int map_y = this.Location.Y;
                            
                            Form2 form2 = new Form2();
                            form2.StartPosition = FormStartPosition.Manual;
                            form2.Location = new Point(map_x,map_y);
                            form2.Show();
                            this.Hide();
                        }

                        reader.Close();
                        cn.Close();
                    }
                    catch (MySqlException e1)
                    {
                        label2.Text = "データベースに接続できませんでした";
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show(e1.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

        private void Form1_FormClosing(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

    }
}
